package com.cbtx.module_sp.ui.activity.base

import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.provider.Settings
import android.view.MotionEvent
import android.view.WindowManager
import androidx.lifecycle.ViewModelProviders
import com.cbtx.module.base.ui.BaseActivity
import com.cbtx.module.base.vm.BaseVM
import com.cbtx.module.common.dialog.CommentConfirmDialog
import com.cbtx.module_sp.TaskService
import com.txcb.lib.base.AppManager

abstract class MyBaseVMActivity<VM : BaseVM>() : BaseActivity() {

    companion object {
        const val REQUEST_OVERLAY = 0x001
        const val RQ_WIFI_SET = 0x002
    }

    lateinit var mVM: VM

    val mHandler = Handler()

    override fun onCreate(savedInstanceState: Bundle?) {
        mVM = ViewModelProviders.of(this).get(getPresenterClazz())
        super.onCreate(savedInstanceState)
        TaskService.activityTouchTime = System.currentTimeMillis()
    }


    abstract fun getPresenterClazz(): Class<VM>

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()


    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        TaskService.activityTouchTime = System.currentTimeMillis()
        checkTouch(event)
        return super.onTouchEvent(event)
    }


    private val longTouchHandler = Handler()

    fun checkLongTouch(event: MotionEvent?) {
        val y = event?.y
        val x = event?.x

        if ((x!! >= 1130 && x <= 1280 && y!! >= 0 && y <= 150)) {
            return
        }

        if (event?.actionMasked == MotionEvent.ACTION_DOWN) {
            longTouchHandler.postDelayed({

                val lastActivity = AppManager.getInstance().currentActivity()

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    val panelIntent =
                        Intent(Settings.Panel.ACTION_WIFI)
                    lastActivity?.startActivityForResult(
                        panelIntent,
                        MyBaseVMActivity.RQ_WIFI_SET
                    )
                } else {
                    val intent = Intent()
                    intent.action = "android.net.wifi.PICK_WIFI_NETWORK"
                    intent.putExtra(":settings:show_fragment_as_subsetting", true)
                    lastActivity?.startActivityForResult(intent, MyBaseVMActivity.RQ_WIFI_SET)
                }
            }, 1000 * 5)
        } else if (event?.actionMasked == MotionEvent.ACTION_UP) {
            longTouchHandler.removeCallbacksAndMessages(null)
        }
    }

    //判断是否已连接网络
    open fun isNetworkConnected(context: Context?): Boolean {
        if (context != null) {
            val mConnectivityManager = context
                .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val mNetworkInfo = mConnectivityManager.activeNetworkInfo
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable
            }
        }
        return false
    }


    var topClickCount = 0
    var topClickTime: Long = 0
    fun checkTouch(event: MotionEvent?) {

        if (event?.actionMasked != MotionEvent.ACTION_DOWN) {
            return
        }
        val y = event?.y
        val x = event?.x

        if (x!! >= 1130 && x <= 1280 && y!! >= 0 && y <= 150) {
            //记录为点击 1次


            val time2 = System.currentTimeMillis() - topClickTime
            if (time2 > 1500) {
                topClickCount = 1
            } else {
                topClickCount++
            }


            if (topClickCount >= 15) {

                val localIntent = Intent(Settings.ACTION_SETTINGS)
                startActivity(localIntent)

                topClickCount = 0
            }

            topClickTime = System.currentTimeMillis()
        }
    }


    fun toNetSet() {


        val netSetActivity = AppManager.getInstance().currentActivity()

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val panelIntent = Intent(Settings.Panel.ACTION_WIFI)
            netSetActivity?.startActivityForResult(
                panelIntent,
                MyBaseVMActivity.RQ_WIFI_SET
            )
        } else {
            val intent = Intent()
            intent.action = "android.net.wifi.PICK_WIFI_NETWORK"
            intent.putExtra(":settings:show_fragment_as_subsetting", true)
            netSetActivity?.startActivityForResult(intent, MyBaseVMActivity.RQ_WIFI_SET)
        }

    }


    var commentConfirmDialog: CommentConfirmDialog? = null

    fun showNoNet(isShow: Boolean) {
        if (!isShow) {
            if (commentConfirmDialog != null && commentConfirmDialog!!.isShowing) {
                commentConfirmDialog!!.dismiss()
                commentConfirmDialog = null
            }
            return
        }

        if (commentConfirmDialog != null && commentConfirmDialog!!.isShowing) {
            return
        }

        val lastActivity = AppManager.getInstance().currentActivity()

        if (!Settings.canDrawOverlays(this)) {
            return
        }

        commentConfirmDialog = CommentConfirmDialog(lastActivity!!)

        //只有这样才能弹框
        if (Build.VERSION.SDK_INT >= 26) { //8.0新特性
            commentConfirmDialog!!.window!!.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
        } else {
            commentConfirmDialog!!.window!!.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
        }
        commentConfirmDialog!!
            .setContent("哎呀，暂无网络，请前往设置")
            .setAgree("前往设置")
            .setOnDialogListener(object : CommentConfirmDialog.OnDialogListener {
                override fun onCancel() {

                }

                override fun onAgree() {
                    commentConfirmDialog!!.dismiss()
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        val panelIntent =
                            Intent(Settings.Panel.ACTION_WIFI)
                        lastActivity?.startActivityForResult(
                            panelIntent,
                            MyBaseVMActivity.RQ_WIFI_SET
                        )
                    } else {
                        val intent = Intent()
                        intent.action = "android.net.wifi.PICK_WIFI_NETWORK"
                        intent.putExtra(":settings:show_fragment_as_subsetting", true)
                        lastActivity?.startActivityForResult(intent, MyBaseVMActivity.RQ_WIFI_SET)
                    }
                }
            })
        commentConfirmDialog!!.show()
    }
}